<?php

class ProductController extends BaseController
{
	
	public function getIndex()
	{
		$products = Product::all()->toArray();
		$_link = array("_rel" => "self", "self" => array("href" => ""));
		
		for($i = 0; $i < count($products); $i++)
		{
			$_link['_rel'] = "self";
			$_link['self']['href'] = "http://integrated:85/product/view/" .$products[$i]['id'];
			 
			$products[$i]['_links'] = $_link;
		}
		
		return Response::json($products);
	}
	
	public function getFind($id)
	{
		$product = Product::find($id);
		
		$_link = array("_rel" => "self", "self" => array("href" => ""));
		$_link['self']['href'] = "http://integrated:85/product/view/" .$product['id'];
		$_link['_rel'] = "self";
		$_link['self']['href'] = "http://integrated:85/product/view/" .$product['id'];
		
		$product['_links'] = $_link;
		
		return Response::json($product);
	}
	
	public function getUpdate($id)
	{
		return Response::json(Product::find($id));
	}
	
	public function postDelete($id)
	{
		$product = Product::find($id);
		
		if(empty($product))
		{
			return Response::json("False");
		}
		
		$product->delete();
		
		return Response::json("True");
	}
	
	public function postUpdate()
	{
		$product = Product::find(Input::get('id'));
		
		if(empty($product))
		{
			return Response::json("False");
		}
		
		$product->productName = Input::get('productname');
		$product->retailPrice = Input::get('retailprice');
		$product->costPrice = Input::get('costprice');
		$product->quantity = Input::get('quantity');
		
		$product->save();
		
		return Response::json("True");
	}
	
	public function postAdd()
	{
		$product = new Product;
		
		$product->productName = Input::get('productname');
		$product->retailPrice = Input::get('retailprice');
		$product->costPrice = Input::get('costprice');
		$product->quantity = Input::get('quantity');
		$product->Supplier_id = Input::get('supplier');
		$product->Category_id = Input::get('category');
		
		$product->save();
		
		return Response::json(Product::all());
	}
	
}